﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package GameMode {
    import flash.events.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Events.*;
    import Classes.*;
    import Classes.util.*;
    import Engine.*;
    import Mechanics.GameEventMechanics.*;

    public class GMMechanicPlaceFromStorage extends GMObjectMove {

        private var m_storage:IMechanicUser;
        private var m_slotId:int;
        private var m_itemInstance:ItemInstance;

        public function GMMechanicPlaceFromStorage(_arg1:IMechanicUser, _arg2:int, _arg3:ItemInstance){
            this.m_storage = _arg1;
            this.m_slotId = _arg2;
            this.m_itemInstance = _arg3;
            super(null, null, null, Constants.INDEX_NONE, true);
        }
        override public function onMouseMove(_arg1:MouseEvent):Boolean{
            var _local2:Boolean;
            if (this.m_itemInstance == null){
                return (super.onMouseMove(_arg1));
            };
            if (!m_selectedObject){
                m_highlightedPoint = getMouseTilePos();
                m_selectedObject = (this.m_itemInstance as MapResource);
                m_selectedObject.setOuter(Global.world);
                resetOnPlaceCollisions();
                registerAllOnPlaceCollisions();
                snapPoint(m_highlightedPoint);
                m_selectedObject.setPosition(m_highlightedPoint.x, m_highlightedPoint.y);
            };
            Global.world.citySim.roadManager.updateResource((m_selectedObject as MapResource));
            m_autoRotate = (m_selectedObject as MapResource).autoRotate;
            if (m_autoRotate){
                rotateToRoad((m_selectedObject as MapResource));
            };
            _local2 = super.onMouseMove(_arg1);
            return (_local2);
        }
        override protected function completeObjectDrop():void{
            var _local1:MapResource = (m_selectedObject as MapResource);
            var _local2:Boolean = _local1.isPlacedObjectNonBuilding();
            this.removeItemFromStorage();
            m_selectedObject.setPosition(Math.round(m_releasedPoint.x), Math.round(m_releasedPoint.y));
            m_selectedObject.conditionallyReattach();
            m_selectedObject.onObjectDrop(m_objectStartPos);
            m_objectPlacedSuccess = true;
            if (_local2){
                Sounds.play("click4");
            } else {
                Sounds.play("place_building");
            };
            onObjectDropCompleted();
        }
        protected function removeItemFromStorage():void{
            var _local1:Array = new Array();
            _local1["mode"] = GatedStorageMechanic.MODE_PLACE_FROM_STORAGE;
            _local1["slotId"] = this.m_slotId;
            _local1["mapResource"] = (m_selectedObject as MapResource);
            MechanicManager.getInstance().handleAction(this.m_storage, "GMMechanicStore", _local1);
            Global.ui.dispatchEvent(new GenericObjectEvent(GenericObjectEvent.OBJECT_PLACED_FROM_STORAGE, null));
        }
        override public function removeMode():void{
            super.removeMode();
            Global.ui.dispatchEvent(new GenericObjectEvent(GenericObjectEvent.OBJECT_PLACED_FROM_STORAGE, null));
        }

    }
}//package GameMode 
